<?php
require_once APPLICATION_PATH . '/controllers/LoginController.php';

/**
 * {$this->_enumT}管理控制器
 * @author Administrator
 *
 */
class EnumController extends LoginController
{

	/**
	 * 枚举键
	 * @var string
	 */
	private $_enumV;
	
	/**
	 * 枚举表模型
	 * @var Application_Model_DbTable_Enumdata
	 */
	private $_enumM;
	
	/**
	 * 枚举string
	 * @var string
	 */
	private $_enumT;
	
    public function init()
    {
    	//enum类型 string
    	$this->_enumV = $this->getRequest()->getParam("type");
    	$this->_enumM = new Application_Model_DbTable_Enumdata($this->_enumV);
    	if(!array_key_exists($this->_enumV,$this->_enumM->ENUMARR))die('404');
    	$this->_enumT = $this->_enumM->ENUMARR[$this->_enumV];
    	$this->view->enumT = $this->_enumT;
    	$this->view->enumV = $this->_enumV;
    	
        parent::init();
    }

    /**
     * 列表
     * @see LoginController::indexAction()
     */
    public function indexAction()
    {
    	
    	
        if($this->getRequest()->isPost()){
        	//获取数据
        	$enumM = $this->_enumM;
        	$pageM = $this->_helper->Flexgp();
			$data  = $enumM->jqfetchAll($pageM);
			$data  = $this->view->jqflexgrid($data,"id");
		    $this->_helper->json($data); 
        }else{
        	//获取页面
    		$this->view->title = "{$this->_enumT}列表";
        }
    }

    /**
     * 添加
     * @see LoginController::indexAction()
     */
    public function addAction()
    {
    	$formM = new Application_Form_Base("enum");
    	$formM->setAction("{$this->view->pagePath}/enum/add/type/{$this->_enumV}");
    	
    	if($this->getRequest()->isPost()){
        	//提交数据
        	//json返回的数据
        	$reData = array(
        		"status" => 0,
        		"msg"	 => "失败。"
        	);
        	$data = $this->getRequest()->getParams();
        	if($formM->isValid($data)){
        		$enumM = $this->_enumM;
        		$data  = $formM->getValues(); 
        		//$data["id"] = $data["enumid"];unset($data["enumid"]);
        		//$data['create_user'] = $this->_user_id;	//创建人id
	        	//$data['create_time'] = time(); 			//创建时间
	        	try {
		        	$result = $enumM->insert($data);
		        	$reData["status"]=1;
		        	$reData["msg"]="成功！";
		        	Application_Service_Msg::write(
		        		"{$this->_user_name}添加{$this->_enumT}：{$data["name_cn"]} | {$data["name_en"]}",
		        		Application_Model_Msg::TYPE_USERDO_LOG
		        	);
	        	}catch (Exception $e){
        			$reData["form"] = $formM->render();
	        		$reData["msg"]="数据库错误！" . $e->getMessage();
	        	}
        	}else{
        		$reData["msg"]="表单填写有误！";
        		$reData["form"] = $formM->render();
        	}
        	$this->_helper->json($reData);
        }else{
        	//获取表单
    		$this->view->form = $formM;
        }
        $this->_helper->layout()->disableLayout();
        $this->renderScript("zendform.phtml");
    }
    
    /**
     * 编辑
     * @see LoginController::indexAction()
     */
    public function editAction()
    {
    	$id = intval($this->getRequest()->getParam("id",0));
    	$formM = new Application_Form_Base("enum");
    	$enumM = $this->_enumM;
    	$formM->setAction("{$this->view->pagePath}/enum/edit/type/{$this->_enumV}");
    	
    	if($this->getRequest()->isPost()){
        	//提交数据
        	$reData = array(
        		"status" => 0,
        		"msg"	 => "失败。"
        	);
        	$data = $this->getRequest()->getParams();
        	if($formM->isValid($data)){
        		
        		$data  = $formM->getValues(); 
        		unset($data["enumid"]);
	        	try {
		        	$result = $enumM->update($data, " `id` = {$id} ");
		        	$reData["status"]=1;
		        	$reData["msg"]="成功！";
		        	Application_Service_Msg::write(
		        		"{$this->_user_name}修改{$this->_enumT}：{$data["name_cn"]} | {$data["name_en"]}",
		        		Application_Model_Msg::TYPE_USERDO_LOG
		        	);
	        	}catch (Exception $e){
        			$reData["form"] = $formM->render();
	        		$reData["msg"]="数据库错误！" . $e->getMessage();
	        	}
        	}else{
        		$reData["msg"]="表单填写有误！";
        		$reData["form"] = $formM->render();
        	}
        	$this->_helper->json($reData);
        }else{
        	//获取表单
        	$data = $enumM->fetchRow(" `id` = {$id} ");
        	if($data){
        		$formM->setDefaults($data->toArray());
        	}
    		$this->view->form = $formM;
        }
        $this->_helper->layout()->disableLayout();
        $this->renderScript("zendform.phtml");
    }
    
	/**
	 * 删除
	 */
	public function delAction()
	{
		$reData = array(
			"status" => 0,
			"msg"    => "参数错误"
		);	    
    	$id = (array)$this->getRequest()->getParam('id');
    	if($id){
			$enumM = $this->_enumM;
			$where = $enumM->getAdapter()->quoteInto('`id` IN(?)', $id);
    		if($result = $enumM->delete($where)){
    			$reData = array(
					"status" => 1,
    		 		"msg"    => "成功删除{$result}条数据！"
    		 	);	  
    		 }
	    }else{
	        	
	    }
	    $this->_helper->json($reData);
   }
}

